﻿package cn.poco.pepsi.activity.search.thread;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import cn.poco.pepsi.Service_impl.impl.NearImpl;
import cn.poco.pepsi.Service_impl.impl.RankImpl;
import cn.poco.pepsi.Service_impl.impl.SearchImpl;
import cn.poco.pepsi.Service_impl.impl.TopicImpl;
import cn.poco.pepsi.bean.Area;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.dao.CacheDbService;


public class SearchUtil {
	private final static String TAG = "SearchUtil";
	
	//获取城市区域
	public static ArrayList<Area> getArea(Context context,String citycode){
		ArrayList<Area> areaData = new ArrayList<Area>();
		CacheDbService db = new CacheDbService(context);
		try{
			if(db.checkArea(citycode)<=0){
				SearchImpl search = new SearchImpl();
				areaData = search.getArea(citycode);
				areaData.add(0,new Area("000000", "全部地区"));
				db.saveArea(citycode, areaData);
			}else{
				areaData = (ArrayList<Area>) db.getArea(citycode);
			}
		}catch (Exception e) {
			Log.i(TAG, e.toString());
			e.printStackTrace();
		}
		return areaData;
	}
	
	//获取城市区域商区
	public static ArrayList<Area> getSubArea(Context context,String citycode,String lid){

		ArrayList<Area> areaSubData = new ArrayList<Area>();
		try{
			SearchImpl search = new SearchImpl();
			areaSubData = search.getSubArea(citycode, lid);
			CacheDbService db = new CacheDbService(context);
			areaSubData.add(0,new Area("222222","全部地区"));
			areaSubData.add(1,new Area("111111", db.getAreaNameById(lid)));
		}catch (Exception e) {
			Log.i(TAG, e.toString());
			e.printStackTrace();
		}
		return areaSubData;
	
	
	}
	
	//获取TOP100餐厅
	public static ArrayList<Restaurant> getTopRes(String citycode,int CURRENT_PAGE,
					ArrayList<Restaurant> restaurant,Boolean clear)throws Exception{
		RankImpl rank = new RankImpl();
			if(clear){
				restaurant = rank.GetTopRes(citycode, CURRENT_PAGE, 10);
			}else{
				restaurant.addAll(rank.GetTopRes(citycode, CURRENT_PAGE, 10));
			}
			
		
		return restaurant; 
	}
	
	//根据专题ID获取餐厅
	public static ArrayList<Restaurant> getTopicRes(String tid,int CURRENT_PAGE,
			Boolean clear,ArrayList<Restaurant> restaurant)throws Exception{
		TopicImpl topic = new TopicImpl();
	
			if(clear){
				restaurant = topic.getResByTid(String.valueOf(tid), CURRENT_PAGE, 10);
			}else{
				restaurant.addAll(topic.getResByTid(String.valueOf(tid), CURRENT_PAGE, 10));
			}
		return restaurant;
	}
	
	//根据GPS地理坐标获取餐厅列表
	public static ArrayList<Restaurant> getResByLocation(String citycode,ArrayList<Restaurant> restaurant
			,Boolean clear,double lat,double lon,int CURRENT_PAGE,int dis,int vouch,long dishid,float d)
			throws Exception{
		NearImpl near = new NearImpl();
			if(lat >0 && lon >0){
				if(clear){
					ArrayList<Restaurant> temprestaurant = (ArrayList<Restaurant>) 
						near.getResByGps(citycode,lat, lon, CURRENT_PAGE, 10,dis, vouch, dishid, d);
					restaurant.clear();
					restaurant.addAll(temprestaurant);
				}else{
					restaurant.addAll(near.getResByGps(citycode,lat, lon, CURRENT_PAGE, 10,dis, vouch, dishid, d));
				}	
			}
		
						
		return restaurant; 
	}
	
	// 根据商区代码获取美食列表
	public static  ArrayList<Restaurant> getResByCircleCode(ArrayList<Restaurant> restaurant,String citycode
			,int CURRENT_PAGE,String dis,String vouch,String locationid,String dishid,
			String key,String lid,String dish,Boolean clear)throws Exception {
	
			SearchImpl srarch = new SearchImpl();
			if(clear){
				ArrayList<Restaurant> temprestaurant = srarch.getFilterRestaurant(key, lid, dish,
						citycode, CURRENT_PAGE, "10", dis, vouch, locationid,dishid);
				restaurant.clear();
				restaurant.addAll(temprestaurant);
			}else{
				restaurant.addAll(srarch.getFilterRestaurant(key, lid, dish,
						citycode, CURRENT_PAGE, "10", dis, vouch, locationid,dishid));
			}
		
		return restaurant;
		
	}

}
